package com.un.ebs.order.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.un.ebs.order.domain.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.un.ebs.order.domain.OrderExt;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author liubo
 * @since 2020-05-15
 */
@Component
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 没有发送订单
     *
     * @param time
     * @return
     */
    @SqlParser(filter = true)
    @Select("select top 10 * from tb_order where tenant_id ='8b23d64b7bc118e06d1edbefc6b35c5e' and len(linkman_phone)=11 and accept_status=1 and send_notify_status=0 and accept_time>=#{time}")
    List<Order> selectNotNotifyRecord(Date time);

    @SqlParser(filter = true)
    @Select("select * from tb_order where id=#{id}")
    Order selectNotTenantById(String id);

    @Select("select * from tb_order_ext where id=#{id}")
    OrderExt selectViewById(String id);

    @SqlParser(filter = true)
    @Update("update tb_order set send_notify_status=1 where id=#{id}")
    int updateNotifyStatus(String id);

    @SqlParser(filter = true)
    @Select({"<script>" +
            "select * from tb_order where " +
            "cust_id in " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'> #{id}" +
            "</foreach>" +
            " and tenant_id=#{tenantId}" +
            "</script>"})
    List<Order> selectByCustomer(String[] ids, String tenantId);

    @SqlParser(filter = true)
    @Select("select * from tb_order where cust_id=#{id} and is_delete=0")
    List<Order> selectByCustId(String id);

    @Select("SELECT ISNULL(MAX(CAST(RIGHT(code,4) AS int)),0)+1 FROM dbo.tb_order WHERE CONVERT(VARCHAR(8), create_time, 112) = CONVERT(VARCHAR(8), GETDATE(), 112)")
    int maxCode();

    @Select("SELECT COUNT(*) FROM dbo.tb_work_order WHERE accept_status=1 and create_order_item_id IN (SELECT id FROM dbo.tb_order_item WHERE order_id=#{orderId})")
    int workOrderAcceptCount(String orderId);
}
